"""用于保存发送短信的异步任务"""
import logging

# 参数'django'须与配置文件中的日志文件配置中的日志器名'django'一致
from celery_tasks.main import app

logger = logging.getLogger('django')


@app.task(name='send_sms_code')
def send_sms_code(mobile, code, expires, template_id):
    """
    发送短信验证码
    :param mobile: 手机号
    :param code: 验证码
    :param expires: 有效期
    :return: None
    """
    try:
        # return = CCP.send_template_sms(mobile, [code, expires], template_id)
        result = 0
        print(code)
    except Exception as e:
        logger.error("发送验证码短信[异常][ mobile: %s, message: %s ]" % (mobile, e))
    else:
        if result == 0:
            logger.info("发送验证码短信[正常][ mobile: %s ]" % mobile)
        else:
            logger.warning("发送验证码短信[失败][ mobile: %s ]" % mobile)
